<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,initial-scale=1.0,width=device-width"/>
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title>大奔农庄</title>
    <link rel="stylesheet" type="text/css" href="../../css/api.css"/>
    <link rel="stylesheet" type="text/css" href="../../css/aui.css"/>
    <link rel="stylesheet" type="text/css" href="../../css/style.css"/>    
    <link rel="stylesheet" href="../../css/swiper.min.css">
    <link rel="stylesheet" href="../../font/iconfont.css">
</head>
<body>
	<div class="aui-content">
	    <div class="aui-list aui-media-list aui-margin-b-15">
		    <div class="aui-list-item aui-list-item-arrow">
	            <div class="aui-media-list-item-inner">
	                <div class="dd-info-wei">
	                     <i class="aui-iconfont aui-icon-location aui-font-size-20"></i>
	                </div>
	                <div class="aui-list-item-inner" onclick="selectadr();">
	                    <div class="aui-list-item-text">
	                        <div class="aui-list-item-title" id="tj_adr_linkman"></div>
	                        <div class="aui-list-item-right" id="tj_adr_contact"></div>
	                    </div>
	                    <div class="aui-list-item-title" id="tj_adr_address"></div>
	                </div>
	            </div>
       	    </div>
    	</div>
    </div>
    <!-- 订单详情 -->
    <div class="aui-card-list-content">
        <ul class="aui-list aui-media-list">
            <li class="aui-list-item aui-list-item-middle">
                <div class="aui-media-list-item-inner">
                    <div class="aui-list-item-media">
                        <img src="../../image/shuoguo1.png" class=" aui-list-img-sm" id="goods_picurl">
                    </div>
                    <div class="aui-list-item-inner">
                        <div class="aui-list-item-text">
                            <div class="aui-list-item-title aui-font-size-16" id="goods_title"> </div>
                        </div>
                        <div class="aui-list-item-text aui-text-success aui-font-size-16" id="goods_danjia"> </div>
                    </div>
                </div>
            </li>
        </ul>
    </div>
    <!-- 订单价格详情列表 -->
    <div class="aui-content aui-margin-t-15">
	    <ul class="aui-list aui-list-in">
	        <li class="aui-list-item">
	            <div class="aui-list-item-inner">
	                <div class="aui-list-item-title aui-font-size-14">数量</div>
	                <div class="aui-list-item-right aui-font-size-14">
	                	 <div class="tb1_td5">
							<input id="min1" type="button" value="-" onclick="buyjian();"/>
							<input id="buy_number" type="text" readonly value="1"/>
							<input id="add1" type="button" value="+" onclick="buyjia();"/>
						</div>
	                </div>
	            </div>
	        </li>
	        <li class="aui-list-item">
	            <div class="aui-list-item-inner">
	                <div class="aui-list-item-title aui-font-size-14">运费</div>
	                <div class="aui-list-item-right aui-font-size-14" id="order_yunfei">￥10.00</div>
	            </div>
	        </li>
	        <li class="aui-list-item">
	            <div class="aui-list-item-inner">
	                <div class="aui-list-item-title aui-font-size-14">总计</div>
	                <div class="aui-list-item-right aui-font-size-14" id="order_zongji">￥10.00</div>
	            </div>
	        </li>
	    </ul>
	    <ul class="aui-list aui-list-in aui-margin-t-15">
	        <li class="aui-list-item">
	            <div class="aui-list-item-inner">
	            	<input type="text" placeholder="给卖家留言（选填）" class="aui-font-size-14" id="remark"/>
	            </div>
	        </li>
	    </ul>
	 </div>
	<div class="padding-b-55"></div>
	<!--底部-->
	<footer class="aui-bar aui-bar-tab spinfo-foot">
		<a class="aui-col-xs-4 aui-bg-success aui-pull-right" onclick="doXiadan();">提交订单</a>
		<div class="aui-col-xs-3 aui-pull-left" style="margin-top: 3px;">
			<a id="kefu_phone"><i class="iconfont icon-dianhua-copy aui-margin-t-5 jinrong-dianh"></i></a>
			<a href="javascript:;" id="kefu_qq" onclick="kefu();"><i class="iconfont icon-qq aui-margin-l-10"></i></a>
		</div>
		<div class="aui-pull-left aui-col-xs-5">
			<span class="aui-font-size-16 aui-pull-right shifu">总计：<span class="aui-text-warning" id="order_shifukuan">￥0.00</span>
		</div>
	</footer>
</body>
<script type="text/javascript" src="../../script/api.js"></script>
<script type="text/javascript" src="../../script/common.js"></script>
<script type="text/javascript" src="../../script/aui-tab.js" ></script>
<script>
var db_key = 'user',myParam,db_user,adrid,goods_danjia,goods_yunfei;
apiready = function(){
	myParam = api.pageParam;
	//监听是否重新选择了收货地址
	api.addEventListener({
        name: 'selectadr'
    }, function(ret, err) {
        changeAdr(ret.value.newadr);
    });
    getpeizhi();//获取平台配置信息
	getuser();
	loadgoodsinfo();//记载信息
	GetDefaultAdr();//获取默认收货地址
	api.alert({msg: '如需帮助请联系客服！'});
}
//获取用户储存在本地的数据
function getuser() {
    db_user = $api.getStorage(db_key);
}
//点击增加购买数量
function buyjia(){
	buynumberObj = $api.byId("buy_number");//数量对象
	oldnum = Number($api.val(buynumberObj));//购买数量
	
	newnum = oldnum+1;//当前数量
	$api.val(buynumberObj, newnum);
	
	zongji = Number(goods_danjia * newnum + Number(goods_yunfei));
	$api.html($api.byId('order_shifukuan'), '￥'+zongji);//展示实付款
	$api.html($api.byId('order_zongji'), '￥'+zongji);//展示总计
}
//点击减少购买数量
function buyjian(){
	buynumberObj = $api.byId("buy_number");//数量对象
	oldnum = Number($api.val(buynumberObj));//购买数量
	if(oldnum != 1){
		newnum = oldnum-1;//当前数量
		$api.val(buynumberObj, newnum);
		
		zongji = parseFloat(goods_danjia * newnum + Number(goods_yunfei));
		$api.html($api.byId('order_shifukuan'), '￥'+zongji);//展示实付款
		$api.html($api.byId('order_zongji'), '￥'+zongji);//展示总计
	}
	return;
}
//加载信息
function loadgoodsinfo() {
    var useParam={};
    useParam.rtk = RSapi.myrtk;
    useParam.id = myParam.gid;//商品id
    
    api.ajax({
        url: RSapi.base+RSapi.shangchenginfo,
        method: 'post',
        cache: false,
        timeout: 30,
        dataType: 'json',
        data: { values: useParam },
    }, function(ret, err){
        if(err){
            //api.toast({msg: JSON.stringify(err)});
            api.alert({msg: err.msg});
        }else {
            if (ret.status>0) { 
                $api.attr($api.byId("goods_picurl"),'src',ret.goodsinfo.picurl);//商品缩略图
                $api.html($api.byId("goods_title"),ret.goodsinfo.title);//显示商品名称
                $api.html($api.byId("goods_danjia"),'￥'+ret.goodsinfo.price+'/'+ret.goodsinfo.unit);//显示售价
                $api.html($api.byId("order_yunfei"),'￥'+ret.goodsinfo.yunfei);//显示运费
                $api.html($api.byId("order_zongji"),'￥'+ret.goodsinfo.price);//显示商品名称
                $api.html($api.byId("order_shifukuan"),'￥'+parseFloat(ret.goodsinfo.zongji));//显示总计
                goods_danjia = ret.goodsinfo.price;//商品单价
                goods_yunfei = ret.goodsinfo.yunfei;//商品运费
                api.parseTapmode();
            }else {
                //失败了
                api.toast({msg: ret.info});
            }
        }
    });
}
//下单操作
function doXiadan() {
	if(adrid){
		var count = parseInt(document.getElementById("buy_number").value);//产品数量
	    api.showProgress();
	    var useParam = {
	    	rtk : RSapi.myrtk,
	        mytoken : db_user.mytoken,
	        goods_num : $api.byId('buy_number').value,
	        gid : myParam.gid,
	        adrid : adrid,
	        flag:1,
	        remark:$api.byId('remark').value
	    };
	    useParam.adrid = adrid;
	    api.ajax({
	        url: RSapi.base+RSapi.OrderTijiao,
	        method: 'post',
	        cache: false,
	        timeout: 30,
	        dataType: 'json',
	        data: { values: useParam }, 
	    }, function(ret, err){ 
	        if (err) { 
	            api.alert({msg:err.msg});
	        }else if (ret.status) {
	            api.toast({msg:ret.info});
	             //发送购物车更改事件
	            api.sendEvent({
	                name: 'goods_tocar',
	                extra: {}
	            });
	            
	            //发送订单列表更改事件
	            api.sendEvent({
	                name: 'order_list_change',
	                extra: {}
	            });
	           
	            //打开支付页面
	        	openDopay(ret.oid);
	            
	            //关闭当前页面 (提交订单的页面)
	            api.closeWin();
	        }else { 
	            api.toast({msg:ret.info});
	        }
	        api.hideProgress();
	    });
	}else{
		api.toast({
	        msg:'请设置收货地址'
        });
	}    
}
//获取用户默认的收货地址
function GetDefaultAdr() {
    var useParam = {
    	rtk : RSapi.myrtk,
        mytoken : db_user.mytoken,
    };
    
    api.ajax({
        url: RSapi.base+RSapi.OrderAdr,
        method: 'post',
        cache: false,
        timeout: 30,
        dataType: 'json',
        data: { values: useParam },
    }, function(ret, err){
        if(err){
            //api.toast({msg: JSON.stringify(err)});
            api.alert({msg: err.msg});
        }else {
            if (ret.status>0) { 
                //成功了swiper-slide-img
                $api.html($api.byId("tj_adr_linkman"),ret.adrinfo.linkman);//显示售价
                $api.html($api.byId("tj_adr_address"),ret.adrinfo.address);//显示商品名称
                $api.html($api.byId("tj_adr_contact"),ret.adrinfo.contact);//显示售价
                adrid = ret.adrinfo.id;
            }else {
                //失败了
                api.toast({msg: ret.info});
            }
        }
    });
}
//获取平台配置信息
function getpeizhi() {
    var useData = {
        rtk : RSapi.myrtk,
    };
    api.ajax({
        url: RSapi.base+RSapi.PTdetail,
        method: 'post',
        cache: false,
        timeout: 30,
        dataType: 'json',
        data: { values: useData },
    }, function(ret, err){
        if (err) {
            api.toast({msg:err.msg});
        }else {
            if (ret.status == 1) {
            	//客服电话
                $api.attr($api.byId("kefu_phone"),'href','tel:'+ret.kefu_phone);  
                //客服qq
                $api.attr($api.byId("kefu_qq"),'data-qq',ret.kefu_qq);        
            }
        }
    });
    return false;
}
//更换收货地址
function selectadr() {
    api.openWin({
        name : "address",
        url : '../win/address_win.html',
        pageParam: {doselect: true,tishi:'1'}
    });
}
//更新页面所显示的收货地址
function changeAdr(adrobj) {	
    adrid = adrobj.adrid;
    $api.html($api.byId('tj_adr_linkman'), adrobj.linkman);
    $api.html($api.byId('tj_adr_address'), adrobj.address);
    $api.html($api.byId('tj_adr_contact'), adrobj.contact);
}
//打开支付页面
function openDopay(id){
	api.openWin({
        name: 'win_dopay',
        url: '../win/dopay_win.html',
        pageParam: {oid:id}
    });
}
//打开qq
function kefu() {
	var number = $api.attr($api.byId('kefu_qq'), 'data-qq');
	api.openWin({
		name : 'win_webpage',
		url : 'webpage.html',
		pageParam : {
			title:'在线客服',
			url:'http://wpa.qq.com/msgrd?v=3&uin='+number+'&site=qq&menu=yes'
		}
	});
}
</script>
</html>